﻿using CustomModel;
using DeskInterfaceBLL;
using HeyPiPi_Desk.Base;
using HeyPiPi_Desk.Domain;
using MaterialDesignThemes.Wpf;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Win32;
using Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using Utility;

namespace HeyPiPi_Desk.ViewModels
{
    public class ShopRegistViewModel: BaseNotification
    {
        #region 字段/属性

        private readonly AuthBLL authBLL = new AuthBLL();

        public Window MainWindow { get; set; }

        public DelegateCommand Submit_Cmd { get; set; }

        private string _detailAddress;

        /// <summary>
        /// 详细地址
        /// </summary>
        public string DetailAddress
        {
            get { return _detailAddress; }
            set { _detailAddress = value;this.RaisePropertyChanged(() => this.DetailAddress); }
        }

        /// <summary>
        /// 关闭窗口命令
        /// </summary>
        public DelegateCommand CloseWindow_Cmd { get; set; }

        private area _selectedArea;

        /// <summary>
        /// 被选择的区
        /// </summary>
        public area SelectedArea
        {
            get { return _selectedArea; }
            set { _selectedArea = value; this.RaisePropertyChanged(() => this.SelectedArea); }
        }


        private city _selectedCity;

        /// <summary>
        /// 被选择的城市
        /// </summary>
        public city SelectedCity
        {
            get { return _selectedCity; }
            set { _selectedCity = value;this.RaisePropertyChanged(() => this.SelectedCity); }
        }


        private province _selectedProvince;

        /// <summary>
        /// 被选择的省份
        /// </summary>
        public province SelectedProvince
        {
            get { return _selectedProvince; }
            set { _selectedProvince = value;this.RaisePropertyChanged(() => this.SelectedProvince); }
        }


        private BitmapImage _shopInImg;

        /// <summary>
        /// 店面室内照
        /// </summary>
        public BitmapImage ShopInImg
        {
            get { return _shopInImg; }
            set { _shopInImg = value;this.RaisePropertyChanged(() => this.ShopInImg); }
        }

        private BitmapImage _shopHeaderImg;

        /// <summary>
        /// 店面门头照
        /// </summary>
        public BitmapImage ShopHeaderImg
        {
            get { return _shopHeaderImg; }
            set { _shopHeaderImg = value; this.RaisePropertyChanged(() => this.ShopHeaderImg); }
        }


        private BitmapImage _businessImg;
        /// <summary>
        /// 营业执照
        /// </summary>
        public BitmapImage BusinessImg
        {
            get { return _businessImg; }
            set { _businessImg = value;this.RaisePropertyChanged(() => this.BusinessImg); }
        }


        private BitmapImage _identifyFImg;

        /// <summary>
        /// 身份证号码反面
        /// </summary>
        public BitmapImage IdentifyFImg
        {
            get { return _identifyFImg; }
            set { _identifyFImg = value; this.RaisePropertyChanged(() => this.IdentifyFImg); }
        }


        private BitmapImage _identifyZImg;
        /// <summary>
        /// 身份证号码正面
        /// </summary>
        public BitmapImage IdentifyZImg
        {
            get { return _identifyZImg; }
            set { _identifyZImg = value; this.RaisePropertyChanged(() => this.IdentifyZImg); }
        }


        private PasswordBox _pwdBox;
        /// <summary>
        /// 密码对象
        /// </summary>
        public PasswordBox PwdBox
        {
            get { return _pwdBox; }
            set { _pwdBox = value; this.RaisePropertyChanged(() => this.PwdBox); }
        }

        private PasswordBox _rePwdBox;

        /// <summary>
        /// 验证密码对象
        /// </summary>
        public PasswordBox RePwdBox
        {
            get { return _rePwdBox; }
            set { _rePwdBox = value;this.RaisePropertyChanged(() => this.RePwdBox); }
        }


        private string _email;
        /// <summary>
        /// 邮箱
        /// </summary>
        public string Email
        {
            get { return _email; }
            set { _email = value; this.RaisePropertyChanged(() => this.Email); }
        }

        private string _phoneNum;
        /// <summary>
        /// 手机号吗
        /// </summary>
        public string PhoneNum
        {
            get { return _phoneNum; }
            set { _phoneNum = value;this.RaisePropertyChanged(() => this.PhoneNum); }
        }


        private string identifyNum;
            /// <summary>
            /// 身份证号码
            /// </summary>
        public string IdentityNum
        {
            get { return identifyNum; }
            set { identifyNum = value; this.RaisePropertyChanged(() => this.IdentityNum); }
        }


        private string _realName;

        /// <summary>
        /// 真实姓名
        /// </summary>
        public string RealName
        {
            get { return _realName; }
            set { _realName = value;this.RaisePropertyChanged(() => this.RealName); }
        }


        private readonly ShopBLL shopBLL = new ShopBLL();


        private ObservableCollection<province> _provinces;

        /// <summary>
        /// 保存省份列表
        /// </summary>
        public ObservableCollection<province> Provinces
        {
            get { return _provinces; }
            set { _provinces = value; this.RaisePropertyChanged(() => this.Provinces); }
        }

        private string _shopName;

        /// <summary>
        /// 店铺名
        /// </summary>
        public string ShopName
        {
            get { return _shopName; }
            set { _shopName = value; this.RaisePropertyChanged(() => this.ShopName); }
        }


        private ObservableCollection<city> _cities;

        /// <summary>
        /// 城市列表
        /// </summary>
        public ObservableCollection<city> Cities
        {
            get { return _cities; }
            set { _cities = value; this.RaisePropertyChanged(() => this.Cities); }
        }

        private ObservableCollection<area> _areas;

        /// <summary>
        /// 区/县列表
        /// </summary>
        public ObservableCollection<area> Areas
        {
            get { return _areas; }
            set { _areas = value;this.RaisePropertyChanged(() => this.Areas); }
        }


        /// <summary>
        /// 省份选择命令
        /// </summary>
        public DelegateCommand<object> Province_Changed { get; set; }

        /// <summary>
        /// 城市选择命令
        /// </summary>
        public DelegateCommand<object> City_Changed { get; set; }

        public DelegateCommand<object> OpenFileDialog_Cmd { get; set; }
        #endregion


        #region 私有方法

        /// <summary>
        /// 提交注册方法
        /// </summary>
        private void _submit()
        {
            if (this.SelectedProvince==null)
            {
                this.ShowDialog("省份必须选择");
                return;
            }

            if (this.SelectedCity == null&&this.Cities.Count!=0)
            {
                this.ShowDialog("城市必须选择");
                return;
            }

            if (this.SelectedArea==null&&this.Areas.Count!=0)
            {
                this.ShowDialog("区/县必须选择");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.DetailAddress))
            {
                this.ShowDialog("请填写详细地址");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.ShopName))
            {
                this.ShowDialog("请填写店铺名");
                return;
            }

            if (string.IsNullOrWhiteSpace(this.RealName))
            {
                this.ShowDialog("请填写姓名");
                return;
            }

            if (string.IsNullOrWhiteSpace(this.IdentityNum))
            {
                this.ShowDialog("请填写身份证号码");
                return;
            }
            if (!CommonHelper.ValidateRegex(this.IdentityNum,CommonHelper.Identity_Reg))
            {
                this.ShowDialog("身份证号码格式不正确");
                this.identifyNum = null;
                return;
            }
            if (string.IsNullOrWhiteSpace(this.PhoneNum))
            {
                this.ShowDialog("请填写手机号码");
                return;
            }
            if (!CommonHelper.ValidateRegex(this.PhoneNum,CommonHelper.Phone_Reg))
            {
                this.ShowDialog("手机号码格式错误");
                this.PhoneNum = null;
                return;
            }
            if (string.IsNullOrWhiteSpace(this.Email))
            {
                this.ShowDialog("请填写邮箱");
                
                return;
            }
            if (!CommonHelper.ValidateRegex(this.Email,CommonHelper.Email_Reg))
            {
                this.ShowDialog("邮箱格式错误");
                this.Email = null;
                return;
            }
            if (string.IsNullOrWhiteSpace(this.PwdBox.Password))
            {
                this.ShowDialog("请填写密码");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.RePwdBox.Password))
            {
                this.ShowDialog("请填写确认密码");
                return;
            }
            if (this.PwdBox.Password!=this.RePwdBox.Password)
            {
                this.ShowDialog("两次密码不一致");
                return;
            }

            if (this.IdentifyZImg==null)
            {
                this.ShowDialog("请上传身份证正面图片");
                return;
            }
            if (this.IdentifyFImg==null)
            {
                this.ShowDialog("请上传身份证反面图片");
                return;
            }
            if (this.BusinessImg == null)
            {
                this.ShowDialog("请上传营业执照图片");
                return;
            }
            if (this.ShopHeaderImg==null)
            {
                this.ShowDialog("请上传铺面门头照");
                return;
            }
            if (this.ShopInImg==null)
            {
                this.ShowDialog("请上传室内照片");
                return;
            }
            RegistShopModel rsmodel = new RegistShopModel();
            rsmodel.areaid = this.SelectedCity.id;
            rsmodel.bussimg = CommonHelper.ConvertBitMapImageToBase64(this.BusinessImg);
            rsmodel.cityid = this.SelectedCity.id;
            rsmodel.detailaddress = this.DetailAddress;
            rsmodel.email = this.Email;
            rsmodel.identity = this.IdentityNum;
            rsmodel.phone = this.PhoneNum;
            rsmodel.provinceid = this.SelectedProvince.id;
            rsmodel.pwd = CommonHelper.Encryption(this.PwdBox.Password);
            rsmodel.realname = this.RealName;
            rsmodel.shopheaderimg = CommonHelper.ConvertBitMapImageToBase64(this._shopHeaderImg);
            rsmodel.shopinimg = CommonHelper.ConvertBitMapImageToBase64(this.ShopInImg);
            rsmodel.shopname = this.ShopName;
           

            // 开始提交数据
           bool regres =  this.shopBLL.RegistShop(rsmodel, out this.error);
            ValidateError("RegistDialog");
            if (regres)
            {
                /// 请求token
                /// 
                //到达这一步的时候，应该要获取token并且保存
                //关闭窗口
                this.authBLL.ValidateLogin(this.PhoneNum, CommonHelper.Encryption(this.PwdBox.Password),out this.error);
                if (ValidateError("RegistDialog"))
                {

                    this.ShowDialog("注册成功,即将登陆");
                  this._openLoginDialog();
                    this.MainWindow.Close();
                }
             
            }
            else
            {
                //注册失败后什么都不做
            }

        }

        private async void _openLoginDialog()
        {
            var view = new LoginDialog();
            await DialogHost.Show(view, "RootDialog");
        }

        private void _initFunc()
        {
            //
         
              var _provinces_temp =  this.shopBLL.GetProvinceList(out this.error);
            this.Provinces = new ObservableCollection<province>(_provinces_temp);
            this.ValidateError("RegisterDialog");
            this.Province_Changed = new DelegateCommand<object>((obj) =>
            {
                if (obj==null)
                {
                    return;
                }
                if (obj is province _p)
                {
                    //区/县要被重置
                    // 搜索城市
                    if (_p.provinceID!=null)
                    {
                        var res = this.shopBLL.GetCityByProvinceId(_p.provinceID, out this.error);
                        if (this.ValidateError("RegisterDialog"))
                        {
                            this.Cities = new ObservableCollection<city>(res);
                        }
                        else
                        {
                            this.Cities = new ObservableCollection<city>();
                        }
                    }
                }

            });
            this.City_Changed = new DelegateCommand<object>((obj) =>
            {
                if (obj==null)
                {
                    return;
                }
                if (obj is city _c)
                {
                    // 
                    if (_c.cityID!=null)
                    {
                        var res = this.shopBLL.GetAreaByCityId(_c.cityID, out this.error);
                        if (this.ValidateError("RegisterDialog"))
                        {
                            this.Areas = new ObservableCollection<area>(res);
                        }
                        else
                        {
                            this.Areas = new ObservableCollection<area>();
                        }
                    }
                }
            });
            this.Submit_Cmd = new DelegateCommand(() =>
            {
                this._submit();
            });
            this.OpenFileDialog_Cmd = new DelegateCommand<object>((obj) =>
            {
                int type = Convert.ToInt32(obj);
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Title = "选择图片";
                ofd.Multiselect = false;
                ofd.Filter = "图片文件(*.jpg,*.gif,*.bmp)|*.jpg|*.gif|*.bmp";
                ofd.RestoreDirectory = true;
               if( ofd.ShowDialog()==true)
                {
                    string imgpath = ofd.FileName;
                    string imgbase = CommonHelper.ImgToBase64String(imgpath);
                    switch (type)
                    {
                        case 1:
                            this.IdentifyZImg= CommonHelper.ConvertBase64ToBitMapImage(imgbase);
                            break;
                        case 2:
                            this.IdentifyFImg = CommonHelper.ConvertBase64ToBitMapImage(imgbase);
                            break;
                        case 3:
                            this.BusinessImg = CommonHelper.ConvertBase64ToBitMapImage(imgbase);
                            break;
                        case 4:
                            this.ShopHeaderImg = CommonHelper.ConvertBase64ToBitMapImage(imgbase);
                            break;
                        case 5:
                            this.ShopInImg = CommonHelper.ConvertBase64ToBitMapImage(imgbase);
                            break;
                        default:
                            break;
                    }
                  
                }

                
            });
            this.CloseWindow_Cmd = new DelegateCommand(() =>
            {
                if (GlobalParam.user==null)
                {
                    var view = new LoginDialog();
                DialogHost.Show(view, "RootDialog");
                }
            });
        }


        #endregion

        #region 共有方法
        public ShopRegistViewModel()
        {
            this._initFunc();
        }

        #endregion
    }
}
